package com.shamar.blog.service.impl;


import com.shamar.blog.dao.BlogCategoryDao;
import com.shamar.blog.dto.BlogCategoryExecution;
import com.shamar.blog.entity.BlogCategory;
import com.shamar.blog.enums.BlogCategoryStateEnum;
import com.shamar.blog.exceptions.BlogCategoryOperationException;
import com.shamar.blog.service.BlogCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 *
 * @author fxm
 * @date
 */
@Service
public class BlogCategoryServiceImpl implements BlogCategoryService {
	@Autowired
	private BlogCategoryDao blogCategoryDao;
	/**
	 * 根据条件返回blogCategory列表,且附上了父类名称
	 * @param blogCategoryCondition
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@Override
	public BlogCategoryExecution getBlogCategoryList(BlogCategory blogCategoryCondition, int pageIndex,
                                             int pageSize) {
		List<BlogCategory> blogCategoryList = blogCategoryDao.queryCategoryList(blogCategoryCondition);
		for (BlogCategory blogCategory: blogCategoryList) {
			Integer parentCategoryId = blogCategory.getParentId();
			if (parentCategoryId != null){
			String parentCategoryName = blogCategoryDao.selectByPrimaryKey(parentCategoryId).getCategoryName();
			blogCategory.setParentCategoryName(parentCategoryName);}
		}
		//依据相同的查询条件,返回分类总数
		int count = blogCategoryList.size();
		BlogCategoryExecution be = new BlogCategoryExecution();
		if (blogCategoryList != null) {
			be.setBlogCategoryList(blogCategoryList);
			be.setCount(count);
		} else {
			be.setState(BlogCategoryStateEnum.BANNED.getState());
		}
		return be;
	}
	@Override
	public Integer updateBlogCategory(BlogCategory blogCategory) {
		return blogCategoryDao.updateByPrimaryKeySelective(blogCategory);
	}

	@Override
	public List<BlogCategory> selectTopCategory() {
		return blogCategoryDao.selectTopCategory();
	}

	/**
	 * 根据传入的Category实例新增Category
	 * @param blogCategory
	 * @return
	 */
	@Override
	public BlogCategoryExecution insertSelective(BlogCategory blogCategory) {
		//空值判断
		if(blogCategory == null){
			return new BlogCategoryExecution(BlogCategoryStateEnum.NO_CATEGORY);
		}
		blogCategory.setLastEdittime(new Date());
		blogCategory.setCreatetime(new Date());
		blogCategory.setStatus(1);
		try{
			int effectedNum = blogCategoryDao.insertSelective(blogCategory);
			if (effectedNum > 0){
				return new BlogCategoryExecution(BlogCategoryStateEnum.NORMAL);
			}
			return new BlogCategoryExecution(BlogCategoryStateEnum.NO_CATEGORY);
		}catch (BlogCategoryOperationException e){
			throw new BlogCategoryOperationException("insertCategory error: " + e.getMessage());
		}
	}

	/**
	 * 获取所有的分类
	 * @return
	 */
	@Override
	public BlogCategoryExecution getAllCategory() {
		try{
			BlogCategory blogCategory = new BlogCategory();
			//传入的blogCategory为空则传回所有分类
			List<BlogCategory> blogCategoryList = blogCategoryDao.queryCategoryList(blogCategory);
			BlogCategoryExecution blogCategoryExecution = new BlogCategoryExecution(BlogCategoryStateEnum.SUCCESS);
			blogCategoryExecution.setBlogCategoryList(blogCategoryList);
			return blogCategoryExecution;
		}
		catch (Exception e){
			throw new BlogCategoryOperationException("访问数据失败");
		}
	}

}
